from .models import RoleResource,Role,Resource,UserModel
from rest_framework import serializers
from rest_framework_simplejwt.serializers import TokenObtainPairSerializer
import re


class UserListSerializer(serializers.ModelSerializer):
    role = serializers.CharField(source='role.name')
    department = serializers.CharField(source='department.name')
    position = serializers.CharField(source='position.name')

    class Meta:
        model = UserModel
        fields = ('id', 'username', 'role', 'department', 'position')








class CreateUserSerializer(serializers.ModelSerializer):

    class Meta:
        model = UserModel
        fields = ('username', 'password','phone','role','department','position')


    def create(self, validated_data):
        phone =validated_data.get('phone')
        if not re.match(r"^1[3-9]\d{9}$", phone):
            raise serializers.ValidationError("手机号码格式错误")

        return UserModel.objects.create(**validated_data)





















